using Microsoft.Office.Interop.Excel;
using OrderSystem.SystemFramework;
using System;
using System.Data;
using System.IO;
using System.Reflection;

namespace OrderSystem.FormatOutput
{
	public class ExcelOperationShatter
	{
		private _Application app;

		private _Workbook workBook;

		private _Worksheet workSheet;

		private Range range;

		private object missing = Missing.Value;

		private string _startupPath = null;

		public ExcelOperationShatter(string startupPath)
		{
			this._startupPath = startupPath;
			if (this._startupPath.Substring(this._startupPath.Length - 1, 1) != "\\")
			{
				this._startupPath += "\\";
			}
		}

		private void OpenExcel(string templetFile)
		{
			if (templetFile == null)
			{
				throw new ArgumentException("你指定的参数：templetFile 为空", "templetFile");
			}
			if (!File.Exists(templetFile))
			{
				throw new ArgumentException("你指定的参数：templetFile Excel模板文件不存在", "templetFile");
			}
			this.app = new ApplicationClass();
			this.app.Visible=true;
			this.workBook = this.app.Workbooks.Open(templetFile, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing, this.missing);
			this.workSheet = (_Worksheet)this.workBook.Worksheets[1];
		}

		public void MakeShatterDataToExcel(string templetFile, System.Data.DataTable dtShatterItem, string sShatterNumber, string sDate, string sOperator)
		{
			this.OpenExcel(templetFile);
			string defaultVersionOfOutType = AppSettings.GetDefaultVersionOfOutType("shatter");
			if (defaultVersionOfOutType == "1")
			{
				this.MakeShatterDataToExcel_1(dtShatterItem, sShatterNumber, sDate, sOperator);
			}
		}

		private void MakeShatterDataToExcel_1(System.Data.DataTable dtShatterItem, string sShatterNumber, string sDate, string sOperator)
		{
			int num = 5;
			try
			{
				this.workSheet.Cells[3, 6]= sShatterNumber;
				this.workSheet.Cells[4, 2]= sDate;
				this.workSheet.Cells[4, 6]= sOperator;
				for (int i = 0; i < dtShatterItem.Rows.Count; i++)
				{
					this.workSheet.Cells[num, 1]= "产品ID";
					this.workSheet.Cells[num + 1, 1]= dtShatterItem.Rows[i][0].ToString();
					this.workSheet.Cells[num, 2]= "类型";
					this.workSheet.Cells[num + 1, 2]= dtShatterItem.Rows[i][1].ToString();
					this.workSheet.Cells[num, 3]= "编号";
					this.workSheet.Cells[num + 1, 3]= dtShatterItem.Rows[i][3].ToString();
					this.workSheet.Cells[num, 4]= "规格";
					this.workSheet.Cells[num + 1, 4]= string.Concat(new string[]
					{
						dtShatterItem.Rows[i][4].ToString(),
						(dtShatterItem.Rows[i][5].ToString() == "") ? "" : ("/" + dtShatterItem.Rows[i][5].ToString()),
						"*",
						dtShatterItem.Rows[i][6].ToString(),
						(dtShatterItem.Rows[i][7].ToString() == "") ? "" : ("/" + dtShatterItem.Rows[i][7].ToString())
					});
					this.workSheet.Cells[num, 5]= "补片数量";
					this.workSheet.Cells[num + 1, 5]= dtShatterItem.Rows[i][8].ToString();
					this.workSheet.Cells[num, 6]= "补片面积";
					this.workSheet.Cells[num + 1, 6]= dtShatterItem.Rows[i][9].ToString();
					this.workSheet.Cells[num, 7]= "扣除百分比";
					this.workSheet.Cells[num + 1, 7]= dtShatterItem.Rows[i][10].ToString();
					this.workSheet.Cells[num + 2, 1]= "补片总面积";
					this.workSheet.Cells[num + 3, 1]= dtShatterItem.Rows[i][11].ToString();
					this.workSheet.Cells[num + 2, 2]= "扣薪面积";
					this.workSheet.Cells[num + 3, 2]= dtShatterItem.Rows[i][12].ToString();
					this.workSheet.Cells[num + 2, 3]= "折合面积";
					this.workSheet.Cells[num + 3, 3]= dtShatterItem.Rows[i][13].ToString();
					this.workSheet.Cells[num + 2, 4]= "工作组";
					this.workSheet.Cells[num + 3, 4]= dtShatterItem.Rows[i][14].ToString();
					this.workSheet.Cells[num + 2, 5]= "补片原因";
					this.workSheet.Cells[num + 3, 5]= dtShatterItem.Rows[i][15].ToString();
					this.workSheet.Cells[num + 2, 6]= "客户";
					this.workSheet.Cells[num + 3, 6]= dtShatterItem.Rows[i][16].ToString();
					this.workSheet.Cells[num + 2, 7]= "计划";
					this.workSheet.Cells[num + 3, 7]= dtShatterItem.Rows[i][17].ToString();
					if (i < dtShatterItem.Rows.Count - 1)
					{
						this.range = this.workSheet.get_Range(this.workSheet.Cells[num + 4, 1], this.workSheet.Cells[num + 4, 7]);
						this.range.ClearContents();
						this.range.MergeCells=true;
						this.range.RowHeight=8;
					}
					num += 5;
				}
				if (num > 5)
				{
					this.range = this.workSheet.get_Range(this.workSheet.Cells[5, 1], this.workSheet.Cells[num - 2, 7]);
                this.range.Borders[XlBordersIndex.xlEdgeLeft].LineStyle = 1;
                this.range.Borders[XlBordersIndex.xlEdgeTop].LineStyle = 1;
                this.range.Borders[XlBordersIndex.xlEdgeBottom].LineStyle = 1;
                this.range.Borders[XlBordersIndex.xlEdgeRight].LineStyle = 1;
                this.range.Borders[XlBordersIndex.xlInsideVertical].LineStyle = 1;
                this.range.Borders[XlBordersIndex.xlInsideHorizontal].LineStyle = 1;
            }
				this.workBook.Save();
			}
			catch (Exception ex)
			{
				this.workBook.Save();
				throw ex;
			}
		}

		public void MakeShatterDataToExcel2(string templetFile, System.Data.DataTable dtShatterItem, string sShatterNumber, string sDate, string sOperator)
		{
			int num = 0;
			decimal num2 = 0m;
			int num3 = 6;
			this.OpenExcel(templetFile);
			try
			{
				this.workSheet.Cells[4, 2]= sShatterNumber;
				this.workSheet.Cells[4, 5]= sDate;
				this.workSheet.Cells[4, 9]= sOperator;
				for (int i = 0; i < dtShatterItem.Rows.Count; i++)
				{
					this.workSheet.Cells[num3, 1]= dtShatterItem.Rows[i]["客户"].ToString();
					this.workSheet.Cells[num3, 2]= dtShatterItem.Rows[i]["计划"].ToString();
					this.workSheet.Cells[num3, 3]= dtShatterItem.Rows[i]["产品ID"].ToString();
					this.workSheet.Cells[num3, 4]= dtShatterItem.Rows[i]["类型"].ToString();
					this.workSheet.Cells[num3, 5]= dtShatterItem.Rows[i]["补片层名称"].ToString();
					this.workSheet.Cells[num3, 6]= dtShatterItem.Rows[i]["宽"].ToString();
					this.workSheet.Cells[num3, 7]= dtShatterItem.Rows[i]["高"].ToString();
					this.workSheet.Cells[num3, 8]= dtShatterItem.Rows[i]["补片数量"].ToString();
					this.workSheet.Cells[num3, 9]= dtShatterItem.Rows[i]["补片面积"].ToString();
					this.workSheet.Cells[num3, 10]= dtShatterItem.Rows[i]["工作组"].ToString() + "：" + dtShatterItem.Rows[i]["补片原因"].ToString();
					num += Convert.ToInt32(dtShatterItem.Rows[i]["补片数量"]);
					num2 += Convert.ToDecimal(dtShatterItem.Rows[i]["补片面积"].ToString());
					num3++;
				}
				this.workSheet.Cells[num3, 7]= "合计：";
				this.workSheet.Cells[num3, 8]= num;
				this.workSheet.Cells[num3, 9]= num2;
				num3++;
				if (num3 > 6)
				{
					this.range = this.workSheet.get_Range(this.workSheet.Cells[5, 1], this.workSheet.Cells[num3 - 1, 10]);
                    this.range.Borders[XlBordersIndex.xlEdgeLeft].LineStyle = 1;
                    this.range.Borders[XlBordersIndex.xlEdgeTop].LineStyle = 1;
                    this.range.Borders[XlBordersIndex.xlEdgeBottom].LineStyle = 1;
                    this.range.Borders[XlBordersIndex.xlEdgeRight].LineStyle = 1;
                    this.range.Borders[XlBordersIndex.xlInsideVertical].LineStyle = 1;
                    this.range.Borders[XlBordersIndex.xlInsideHorizontal].LineStyle = 1;
                }
				this.workBook.Save();
			}
			catch (Exception ex)
			{
				this.workBook.Save();
				throw ex;
			}
		}
	}
}
